package cn.ekuma.epos.oa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.epos.module.OA_UI_Swing_Model;
import cn.ekuma.epos.oa.model.AttenceExtTableModel;
import cn.ekuma.epos.panel.AbstractJModelReportFinishingPanel;
import cn.ekuma.epos.util.AbstractRuleChecker;

import com.openbravo.bean.Attribute;
import com.openbravo.bean.AttributeValue;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.bean.oa.AttenceExt;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.def.SystemAttribute;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.pos.model.AbstractPosSwingModel;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;

public class JAttencePanel extends AbstractJModelReportFinishingPanel<AttenceExt> {

	
	private ComboBoxValModel m_UserInfoModel,attenceTypeModel;
	private JComboBox m_jUsern;
	private JComboBox m_jAttenceType;
	AbstractRuleChecker<AttenceExt> rule=new AbstractRuleChecker<AttenceExt>();
	
	public JAttencePanel() {
		super(new AttenceExtTableModel());
	}

	@Override
	public String getTitle() {
		return null;
	}

	@Override
	public void activate() throws BasicException {
		 List locationList=null;
	        try {
	            locationList=dlSales.list(UserInfo.class);
	            locationList.add(0, null);
	        } catch (BasicException ex) {
	            new MessageInf(ex).show(this);
	        }
	        
	        m_UserInfoModel = new ComboBoxValModel(locationList);
	        m_jUsern.setModel(m_UserInfoModel); // para que lo refresque
	        
	        try {
	            locationList=dlSales.list(AttributeValue.class, Attribute.class,SystemAttribute.OA_ATTENCE.getId());
	            locationList.add(0, null);
	        } catch (BasicException ex) {
	            new MessageInf(ex).show(this);
	        }
	        attenceTypeModel=new ComboBoxValModel(locationList);
	        m_jAttenceType.setModel(attenceTypeModel); // para que lo refresque  
	}

	@Override
	public boolean deactivate() {
		return true;
	}

	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public Object createValue() throws BasicException {
		Object[] value=new Object[8];
		
		value[0] = QBFCompareEnum.COMP_NONE;
		value[1] = null;
		value[2] = QBFCompareEnum.COMP_NONE;
		value[3] = null;
		
		UserInfo selectUser=(UserInfo) m_UserInfoModel.getSelectedItem();
		if(selectUser!=null){
			    value[4] = QBFCompareEnum.COMP_EQUALS;
		        value[5] = selectUser.getId();
	    }else{
				value[4] = QBFCompareEnum.COMP_NONE;
				value[5] = null;
		}
		
		AttributeValue selectAttenceType=(AttributeValue) attenceTypeModel.getSelectedItem();
		if(selectAttenceType!=null){
		    value[6] = QBFCompareEnum.COMP_EQUALS;
	        value[7] = selectAttenceType.getValue();
        }else{
			value[6] = QBFCompareEnum.COMP_NONE;
			value[7] = null;
	    }
		
		return value;
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if (beforeFilterParas == null)
			try {
				beforeFilterParas = (Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
		}
		
		// Date range
		Object startdate = e.getStartDate();
		Object enddate = e.getEndDate();

		beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS;
		beforeFilterParas[1] = startdate;
		beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS;
		beforeFilterParas[3] = enddate;
		reQuery();
		
	}

	@Override
	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void configNavigator(JPanel jNavigatorToolBar, JPanel jNavigator) {
		// TODO Auto-generated method stub
	}

	@Override
	public void reQuery() {
		if(beforeFilterParas==null)
			try {
				beforeFilterParas=(Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
			}
		try {
			tableModel.resert(dlSales.listByFilter(AttenceExt.class, beforeFilterParas));
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}

	@Override
	protected void configToolBar(JToolBar jToolBar12) {
		JLabel label = new JLabel("员工：");
		jToolBar12.add(label);
		
	    m_jUsern = new JComboBox();
		jToolBar12.add(m_jUsern);
		
		JLabel label_1 = new JLabel("签到方式");
		jToolBar12.add(label_1);
		
		m_jAttenceType = new JComboBox();
		jToolBar12.add(m_jAttenceType);
		
		JButton ruleCheck=new JButton("规则过滤");
		ruleCheck.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				tableModel.resert(rule.checkRuleIn(dlSystem,"oa.attence",tableModel.getObjs()));
			}
		});
		jToolBar12.add(ruleCheck);
	}

	@Override
	public String getModelName() {	
		return OA_UI_Swing_Model.MODEL_NAME;
	}

	I_DataLogicSystem dlSystem;

	@Override
	public void setModel(AbstractPosSwingModel mode) {
		super.setModel(mode);
		 dlSystem = mode.getDLSystem(); 	
	}

	@Override
	protected void initFinishPanel(I_AppView app) throws BeanFactoryException {
		// TODO Auto-generated method stub
		
	}
	
}

